﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FishTank
{
    public partial class Form1 : Form
    {
        //Global instances
        SafetySensor safetySensor = new SafetySensor();
        OperatingSensor operatingSensor = new OperatingSensor();

        //Water water = new Water();
        Pump pump = new Pump();
        Osmolator osmolator = new Osmolator();

        double waterLevel = 90.0, safetySensorHeight = 95.5, operatingSensorHeight = 90.0;
        double reservoirWaterLevel = 100.0;
        

        public Form1()
        {
            InitializeComponent(); //Build form
        }

        //Set intial values on the form
        private void Form1_Load(object sender, EventArgs e)
        {
            lblWaterLevel.Text = "" + waterLevel + "% full";
            txtBoxMin.Text = "" + operatingSensorHeight;
            txtBoxMax.Text = "" + safetySensorHeight;
            lblSettings.Text = "Warning signal is activated when the \r\nsaftey sensor is activated.";
        }

        private void btnIncreaseWaterLevel_Click(object sender, EventArgs e)
        {
            if (waterLevel < 100)
            {
                waterLevel += 0.1;
                double p = Math.Pow(10, 1);
                waterLevel = (Math.Round(waterLevel * p)) / p;

                lblWaterLevel.Text = "" + waterLevel + "% full";
            }
            else
            {
                MessageBox.Show("Not a valid percentage");
            }
        }

        private void btnDecreaseWaterLevel_Click(object sender, EventArgs e)
        {
            if (waterLevel > 0)
            {
                waterLevel -= 0.1;
                double p = Math.Pow(10, 1);
                waterLevel = (Math.Round(waterLevel * p)) / p;

                lblWaterLevel.Text = "" + waterLevel + "% full";
            }
            else
            {
                MessageBox.Show("Not a valid percentage");
            }
        }

        //To simulate the breaking of the operating sensor
        private void btnBreakOperatingSensor_Click(object sender, EventArgs e)
        {
            if (operatingSensor.isBroken())
            {
                operatingSensor.setBrokenState(false);
                btnBreakOperatingSensor.Text = "Break Operating sensor";
            }
            else
            {
                operatingSensor.setBrokenState(true);
                btnBreakOperatingSensor.Text = "Fix Operating Sensor";
            }
        }

        //Checks are ran evey 500 milliseconds
        private void Checks_Tick(object sender, EventArgs e)
        {
            Image panelImage;
            switch (osmolator.check(operatingSensor, safetySensor, pump, ref  waterLevel, safetySensorHeight, operatingSensorHeight, ref reservoirWaterLevel))
            {
                //Float swtich triggered or/and water is to high
                case 1:
                    panelImage = Properties.Resources.PanelTooHigh;
                    break;
                //Water is too low
                case 2:
                    panelImage = Properties.Resources.PanelTooLow;
                    break;
                //Water is level
                case 0:
                    panelImage = Properties.Resources.PanelLevel;
                    break;
                //Failsafe
                default:
                    panelImage = Properties.Resources.Panel;
                    break;
            }
            pbPanel.Image = panelImage;

            double p = Math.Pow(10, 1);
            waterLevel = (Math.Round(waterLevel * p)) / p;

            lblWaterLevel.Text = "" + waterLevel + "% full";
            osmolator.check(operatingSensor, safetySensor, pump, ref waterLevel, safetySensorHeight, operatingSensorHeight, ref reservoirWaterLevel);

            //reservoirWaterLevel = ((Math.Round(reservoirWaterLevel * p)) / p);
            int tempreservoirWaterLevel = Convert.ToInt32(reservoirWaterLevel);
            lblReservoirWaterLevel.Text = "Water Level: " + tempreservoirWaterLevel + "%";

            if (reservoirWaterLevel == 0)
                btn10MinWait.Visible = true;
            else
                btn10MinWait.Visible = false;

            //10, 139 location at 100%
            //10 15 location at 0%
            //sizes 161, 124
            pbResWater.Location = new Point(25, (Convert.ToInt32(reservoirWaterLevel) * -1) + 301);


            int y = Convert.ToInt32(waterLevel);

            //Maths to work out the location of the water graphic
            pbWater.Location = new Point(-15, ((y * -1) * 2) + 411);

            //To swap the image if fish are in danger
            Image waterImage;
            if (waterLevel < operatingSensorHeight)
            {
                waterImage = Properties.Resources.WaterOverflow;
            }
            else
            {
                waterImage = Properties.Resources.Water;
            }
            pbWater.Image = waterImage;
        }

        private void btnSetWaterLevel_Click(object sender, EventArgs e)
        {
            double temp = 0;
            
            try
            {
                //Test to see if the value is a number
                temp = Convert.ToDouble(txtBoxSetWaterLevel.Text);

                if (temp > 0)//If statment split for diffrent responces
                {
                    if (temp <= 100)//If statment split for diffrent responces
                    {
                        waterLevel = temp;
                    }
                    else
                    {
                        MessageBox.Show("Not a valid percentage");
                        txtBoxSetWaterLevel.Text = "";
                    }
                }
                else
                {
                    MessageBox.Show("Your fish need water, you can't kill them off!");
                    txtBoxSetWaterLevel.Text = "";
                }
            }
            catch
            {
                MessageBox.Show(txtBoxSetWaterLevel.Text + " is not a number");
                txtBoxSetWaterLevel.Text = "";
            }

            
        }

        private void btnMin_Click(object sender, EventArgs e)
        {
            double temp = 0;

            try
            {
                //Test to see if the value is a number
                temp = Convert.ToDouble(txtBoxMin.Text);

                if (temp < safetySensorHeight)
                {
                    if (temp > 0)//If statment split for diffrent responces
                    {
                        if (temp <= 100)//If statment split for diffrent responces
                        {
                            operatingSensorHeight = temp;
                        }
                        else
                        {
                            MessageBox.Show("Not a valid percentage");
                            txtBoxMin.Text = "" + operatingSensorHeight;
                        }
                    }
                    else
                    {
                        MessageBox.Show("Your fish need water, you can't kill them off!");
                        txtBoxMin.Text = "" + operatingSensorHeight;
                    }
                }
                else
                {
                    MessageBox.Show(temp + " is greater than the current max level of " + safetySensorHeight);
                    txtBoxMin.Text = "" + operatingSensorHeight;
                }
            }
            catch
            {
                MessageBox.Show(txtBoxMin.Text + " is not a number");
                txtBoxMin.Text = "" + operatingSensorHeight;
            }
        }

        private void btnSetMax_Click(object sender, EventArgs e)
        {
            double temp = 0;

            try
            {
                //Test to see if the value is a number
                temp = Convert.ToDouble(txtBoxMax.Text);

                if (temp > operatingSensorHeight)
                {
                    if (temp > 0)//If statment split for diffrent responces
                    {
                        if (temp <= 100)//If statment split for diffrent responces
                        {
                            safetySensorHeight = temp;
                        }
                        else
                        {
                            MessageBox.Show("Not a valid percentage");
                            txtBoxMax.Text = "" + safetySensorHeight;
                        }
                    }
                    else
                    {
                        MessageBox.Show("Your fish need water, you can't kill them off!");
                        txtBoxMax.Text = "" + safetySensorHeight;
                    }
                }
                else
                {
                    MessageBox.Show(temp + " is less than the current min level of " + operatingSensorHeight);
                    txtBoxMax.Text = "" + safetySensorHeight;
                }
            }
            catch
            {
                MessageBox.Show(txtBoxMax.Text + " is not a number");
                txtBoxMax.Text = "" + safetySensorHeight;
            }
        }

        private void btnSetReservoirSize_Click(object sender, EventArgs e)
        {
            try
            {
                int reservoirSize = Convert.ToInt32(txtReservoirSize.Text);
                pump.setReservoirSize(reservoirSize);
                reservoirWaterLevel = 100.0;
                lblReservoirWaterLevel.Text = "Water Level: " + reservoirWaterLevel + "%";
            }
            catch
            {
                MessageBox.Show(txtReservoirSize.Text + " is not a valid whole number");
            }
            
        }

        private void btnSetReservoirWaterLevel_Click(object sender, EventArgs e)
        {
            try
            {
                reservoirWaterLevel = Convert.ToDouble(txtReservoirWaterLevel.Text);
                pump.setReservoirWaterLevel(reservoirWaterLevel, ref reservoirWaterLevel);
                lblReservoirWaterLevel.Text = "Water Level: " + reservoirWaterLevel + "%";
            }
            catch
            {
                MessageBox.Show(txtReservoirWaterLevel.Text + " is not a number");
            }
        }
        private void rbSetting1_CheckedChanged(object sender, EventArgs e)
        {
            lblSettings.Text = "Warning signal is activated when the \r\nsaftey sensor is activated.";
            osmolator.setJumperMode(1);
        }
        private void rbSetting2_CheckedChanged(object sender, EventArgs e)
        {
            lblSettings.Text = "Warning signal is activated when the \r\nsaftey sensor is activated and when\r\nthe pump is switched off after10 mins\r\nof pumping";
            osmolator.setJumperMode(2);
        }
        private void rbSetting3_CheckedChanged(object sender, EventArgs e)
        {
            lblSettings.Text = "Warning signals are off.";
            osmolator.setJumperMode(3);
        }

        private void btn10MinWait_Click(object sender, EventArgs e)
        {
            osmolator.toogleWaitTenMins();
        }
    }
}
